<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro" xmlns:dic="http://www.w3.org/1999/xhtml">
<body class="list">
<div th:replace="include/head.html"></div>
<div class="layui-fluid  layui-anim-upbit layui-anim">
    <div class="layui-card">
        <!--查询from 表单-->
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <!--<div class="layui-inline">
                    <label class="layui-form-label">日期范围</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" readonly id="laydate" placeholder=" - ">
                    </div>
                </div>-->
                <div class="layui-inline">
                    <label class="layui-form-label">创建时间</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" readonly id="startTime" name="startTime" placeholder=" - ">
                    </div>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" readonly id="endTime" name="endTime" placeholder=" - ">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>
        <!--表格中的数据 并不显示-->
        <div class="layui-card-body">
            <!--表格外部上侧的按钮-->
            <div style="padding-bottom: 10px;" class="tool-bar">
                <button class="layui-btn layuiadmin-btn-useradmin" data-type="add" shiro:hasPermission="sysUserPermission:add">添加</button>
            </div>
            <!--数据表格 -->
            <table id="table" lay-filter="table"></table>
            <script type="text/html" id="switchTpl">
                <input type="checkbox" name="status" value="{{d.oid}}" lay-skin="switch" lay-text="启用|禁用"
                       lay-filter="status" {{ d.status== 1 ? 'checked' : '' }}>
            </script>
            <!--表格每行的 的操作按钮 -->
            <script type="text/html" id="toolbar">
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"
                   shiro:hasPermission="sysUserPermission:delBean"><i
                        class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>
<script type="text/javascript" th:inline="javascript">
    var table ;

    var userOid = [[${userOid}]];

    layui.use(['jquery','table', 'laydate'], function () {
        var $ = layui.$;
        table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        //日期范围
        laydate.render({
            elem: '#laydate'
            , range: true
            , theme: '#393D49'
        });
        //分两个的时间空间
        var start = laydate.render({
            elem: '#startTime',
            //value: new Date(),
            done: function (value, date) {
                end.config.min = {
                    year: date.year,
                    month: date.month - 1,
                    date: date.date,
                    hours: date.hours,
                    minutes: date.minutes,
                    seconds: date.seconds
                }
            }
        });
        var end = laydate.render({
            elem: '#endTime',
            //value: new Date(),
            done: function (value, date) {
                start.config.max = {
                    year: date.year,
                    month: date.month - 1,
                    date: date.date,
                    hours: date.hours,
                    minutes: date.minutes,
                    seconds: date.seconds
                }
            }
        });
        table.render({
            elem: '#table'
            , id: 'table'
            , loading: true
            , method: 'post'
            , url: 'sysUserPermission/page?userOid='+userOid //数据接口
            , height: "full-190"
            , page: true //开启分页
            , cols: [[ //表头
                {type: 'numbers', fixed: 'left', title: '序号'}
                , {
                    title: '标题', align: 'center'
                    , templet: function (d) {
                        if (d["sysPermission"] !== null) {
                            return "<span>" + d["sysPermission"]["title"] + "</span>";
                        } else {
                            return "";
                        }
                    }
                }, {
                    title: '链接', align: 'center'
                    , templet: function (d) {
                        if (d["sysPermission"] !== null) {
                            return "<span>" + d["sysPermission"]["url"] + "</span>";
                        } else {
                            return "";
                        }
                    }
                }, {
                    title: '权限', align: 'center'
                    , templet: function (d) {
                        if (d["sysPermission"] !== null) {
                            return "<span>" + d["sysPermission"]["permission"] + "</span>";
                        } else {
                            return "";
                        }
                    }
                }, {
                    title: '类型', align: 'center'
                    , templet: function (d) {
                        if (d["sysPermission"] !== null) {
                            if (d["sysPermission"]["type"] === 1) {
                                return "<span>列表</span>";
                            } else if (d["sysPermission"]["type"] === 2) {
                                return "<span>权限</span>";
                            } else {
                                return "";
                            }
                        } else {
                            return "";
                        }
                    }
                }
                , {field: 'status', align: 'center', title: '状态', templet: '#switchTpl'}
                , {field: 'startTime', align: 'center', title: '授权开始时间'}
                , {field: 'endTime', align: 'center', title: '授权结束时间'}
                , {field: 'createTime', align: 'center', title: '创建时间'}
                , {field: 'remark', align: 'center', title: '备注'}
                , {fixed: 'right', width: 80, align: 'center', title: '操作', toolbar: '#toolbar'}
            ]]
        });
        //监听搜索
        form.on('submit(search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('table', {where: field});
        });
        //监听 按钮
        form.on('switch(status)', function(obj){
            var title = "";
            if (obj.elem.checked) {
                obj.elem.checked = !obj.elem.checked;
                form.render();
                title = "不允许启用临时授权！请重新添加临时授权！";
                layer.msg(title);
            } else {
                title = "你确定禁用临时授权吗？如果存在下级临时授权则一并禁用！";
                layer.confirm(title, {icon: 3, title: '提示'},
                    function (index) {//确定回调
                        updateStatus(obj, index, obj.value);
                    }, function (index) {//取消回调
                        obj.elem.checked = !obj.elem.checked;
                        form.render();
                        layer.close(index);
                    }
                );
            }
            function updateStatus(obj, index, oid) {
                $.post("sysUserPermission/updateStatus", {oid: oid}, function (res) {
                    if (res.code === 0) {
                        layer.msg(res.msg);
                        table.reload('table');
                        layer.close(index);
                    } else {
                        obj.elem.checked = !obj.elem.checked;
                        form.render();
                        layer.close(index);
                        layer.msg(res.msg);
                    }
                });
            }
        });
        //监听行工具事件
        //注：tool 是工具条事件名，table 是 table 原始容器的属性 lay-filter="对应的值"
        table.on('tool(table)', function (obj) {
            var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'del') {
                layer.confirm('确定要删除该临时授权吗？如果该授权有下级授权，将一并删除！', function (index) {
                    $.post("sysUserPermission/delBean", {oid: data.oid}, function (res) {
                        //向服务端发送删除指令
                        if (res.code === 0) {
                            layer.msg(res.msg);
                            obj.del(); //删除对应行（tr）的DOM结构
                            layer.close(index);
                            table.reload('table');
                        } else {
                            layer.msg(res.msg);
                        }
                    });
                });
            }
        });
        $('.tool-bar .layui-btn.layuiadmin-btn-useradmin').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        //事件
        var active = {
            add: function () {
                var addSysUserPermission =  editLayer({
                    title:'添加临时授权',
                    url:'sysUserPermission/edit?userOid=' + userOid,
                    area:['800px','600px'],
                    back: callback
                });
            }
        };
    });
    function callback(ifr,index,data){
        var field = data.field;
        $.post("sysUserPermission/saveOrUpdate",field,function (res) {
            if(res.code === 0){
                topLayer.msg(res.msg);
                topLayer.close(index);
                table.reload('table');
            }else {
                topLayer.msg(res.msg);
            }
        })
    }
</script>
</body>
</html>
